module MC.View {
	export class ComProType {
		public constructor() {
			this.init();
		}
		private init() {
			MC.apiD("/api/member/company/com-pro-type-list", {}, (json: any) => {
				console.log(json);
				if(json.length==0){
					this.defaultView();
				}
				for (let i = 0; i < json.length; i++) {
					this.addli(json[i]);
				}
			});
			$('.addlibtn').on('click', () => {
				let obj = {
					cate: $('#cate').val(),
					name: $('#name').val(),
					male: $('#male').val(),
					female: $('#female').val(),
					child: $('#child').val(),
				};
				if (obj.cate == '') {
					MC.msg.alert('新加类别不能为空');
					return;
				}
				if (obj.name == '') {
					MC.msg.alert('新加产品名称不能为空');
					return;
				}
				if (obj.male == '') {
					MC.msg.alert('新加男装不能为空');
					return;
				}
				if (obj.female == '') {
					MC.msg.alert('新加女装不能为空');
					return;
				}
				if (obj.child == '') {
					MC.msg.alert('新加童装不能为空');
					return;
				}
				this.addli(obj);
				$('#cate').val("");
				$('#name').val("");
				$('#male').val("");
				$('#female').val("");
				$('#child').val("");
			});

			$('#proTypeSpan').on('click', '.goodsPanel .delete.button', function () {
				let obj = $(this).parents('.goodsPanel');
				MC.msg.confirm('确认删除本行?', () => {
					obj.remove();
				});
			});

			$('.savebtn').on('click', () => {
				let list = $('#proTypeSpan .goodsPanel');
				let array = [];
				for (let i = 0; i < list.length; i++) {
					let one = $(list[i]);
					let obj = {
						cate: one.find('.cate').val(),
						name: one.find('.name').val(),
						male: one.find('.male').val(),
						female: one.find('.female').val(),
						child: one.find('.child').val(),
					};
					array.push(obj);
				}
				console.log(array);
				MC.apiD("/api/member/company/com-pro-type-set2",{pro_types:JSON.stringify(array)},()=>{
					MC.msg.show('保存成功');
				});
			});

		}
		private addli(obj: any) {
		//	console.log(obj);
			let li = ` <div class="goodsPanel">
			<div class="deviceInner proType">
				<input type="text" class="regCodeCenter cate"  value="`+ obj.cate + `"></div>
			<div class="deviceInner proType"><input type="text" class="regCodeCenter name" value="`+ obj.name + `"></div>
			<div class="deviceInner proType"><input type="number" class="regCodeCenter male" value="`+ obj.male + `"></div>
			<div class="deviceInner proType"><input type="number" class="regCodeCenter female" value="`+ obj.female + `"></div>
			<div class="deviceOuter proType"><input type="number" class="regCodeCenter child" value="`+ obj.child + `"></div>
			<a class="button delete">删除</a>
		</div>`;
			$('#proTypeSpan').append(li);
		}

		/**
		 * 显示默认数据
		 * */
		private defaultView(){
			this.addli({
				cate:'裤子',
				name:'打底裤',
				male:'1',
				female:'1',
				child:'1',
			});
			MC.msg.alert("当前没有已经保存的记录,第一天记录为示例");
		}
	}
}